<template>
  <div class="mainWarp">
    <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="$router.back()" />

    <div class="registerBox">
      <img src="../assets/logo.png" class="imgPro" />

      <div class="itemBox">
        <i class="iconfont icon-ziyuan5"></i>
        <input name="" type="text" placeholder="手机号码" v-model="username" />
      </div>
      <div class="itemBox">
        <i class="iconfont icon-ziyuan"></i>
        <input name="" type="password" placeholder="密码" v-model="password" />
      </div>
      <div class="btnBox btnBox_1"><button name="" type="button" class="btn_1" @click="login" >手机号登录</button></div>

      <div class="btnBox"><button name="" type="button" class="btn_2" @click="wx_login" ><i class="iconfont icon-weixin1"></i>使用微信登录</button></div>
      <div class="noPsd"><!-- <p>Forgot password ?</p> --></div>
    </div>
  </div>
</template>

<script>
import cookies from '../utils/cookies.js';

export default {


  data() {

    return {
      password: '',
      username: ''
    }
  },


  created() {
    this.refreshOpenid();
  },


  methods: {
    login(){



      this.apiPost('/shop.everyone/login', { username: this.username, password: this.password }).then(res => {


        let token = res.data;

        console.log(token)


        if(token != '' || token != null){
          
          cookies.setCookie('token', token);


          let redirect_url = this.$route.query.redirect;

          if (redirect_url != null) {
            this.$router.push(redirect_url);
          } else {
            this.$router.push('/home');
          }
        }


      });

    },
    wx_login() {

      cookies.setCookie('redirect', this.$route.query.redirect);

      location.replace(window.OPENID_URL);
    },
    refreshOpenid() {
      // 1从url获取参数
      let token = this.$route.query.token;

      // alert('location.href' + location.href)
      // alert('uiddd' + uid + '---' + token)
      // 2保存到cookies
      if (token != null) {


        cookies.setCookie('token', token);

       // let redirect_url = this.$route.query.redirect;
        let redirect_url = cookies.getCookie('redirect');

        if (redirect_url != null) {
          this.$router.push(redirect_url);
        } else {
          this.$router.push('/home');
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.mainWarp {
  width: 100%;
  max-width: 512px;
  margin: 0 auto;
  background: #fff;
  height: 100%;
}

.registerBox {
  width: 100%;
  background-size: 100% 100%;
}

.imgPro {
  width: 100px;
  display: block;
  margin: 10px auto;
}

.itemBox {
  width: 75%;
  margin: 5px auto;
  overflow: hidden;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #d1d1d1;
  padding: 5px 0;
}
.itemBox i {
  width: 30px;
  height: 30px;
  float: left;
  margin-top: 5px;
  margin-right: 10px;
  font-size: 24px;
}
.itemBox input {
  width: 70%;
  float: left;
  height: 40px;
  line-height: 40px;
  outline: none;
  border: none;
  color: #3a3a3a;
  font-size: 16px;
  letter-spacing: 0.5px;
}
input::-webkit-input-placeholder {
  color: #3a3a3a;
}
input::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: #3a3a3a;
}
input:-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: #3a3a3a;
}
input:-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  color: #3a3a3a;
}

.btnBox {
  width: 75%;
  margin: 0 auto;
  overflow: hidden;
  margin-top: 15px;
}
.btnBox i{
  font-size: 18px;
}
.btnBox.btnBox_1 {
  margin-top: 40px;
}
.btnBox button {
  width: 100%;
  height: 35px;
  line-height: 35px;
  cursor: pointer;
  color: #fff;
  border: none;
  border-radius: 3px;
  font-size: 16px;
  letter-spacing: 1px;
}
.btn_1 {
  background: #f9671e;
}
.btn_2 {
  background: #1aad19;
}

.noPsd {
  width: 75%;
  margin: 0 auto;
  overflow: hidden;
  margin-top: 40px;
}
.noPsd p {
  color: #8b8b8b;
  font-size: 14px;
  text-align: right;
}
</style>
